package com.sanjiahantang.youjiarole.module.evaluate.presenters;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.text.TextUtils;

import com.sanjiahantang.uilibrary.utils.Utils;
import com.sanjiahantang.youjiarole.base.BaseBean;
import com.sanjiahantang.youjiarole.base.BaseMvpPresenter;
import com.sanjiahantang.youjiarole.base.BaseView;
import com.sanjiahantang.youjiarole.base.CallBack;
import com.sanjiahantang.youjiarole.config.ConfigValue;
import com.sanjiahantang.youjiarole.config.SPConfig;
import com.sanjiahantang.youjiarole.module.common.beans.FileBean;
import com.sanjiahantang.youjiarole.module.common.models.IFileModel;
import com.sanjiahantang.youjiarole.module.common.models.impl.FileModel;
import com.sanjiahantang.youjiarole.module.evaluate.adapters.MeEvaluateAdapter;
import com.sanjiahantang.youjiarole.module.evaluate.beans.EvaGoodsBean;
import com.sanjiahantang.youjiarole.module.evaluate.beans.GoodsAppraiseInitResponse;
import com.sanjiahantang.youjiarole.module.evaluate.models.IEvaluateModel;
import com.sanjiahantang.youjiarole.module.evaluate.models.impl.EvaluateModel;
import com.sanjiahantang.youjiarole.module.evaluate.views.MeEvaluateView;
import com.sanjiahantang.youjiarole.utils.SPUtils;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * Created by zy on 2017/9/6.
 */

public class MeEvaluatePresenter extends BaseMvpPresenter<MeEvaluateView> {

    private IEvaluateModel evaluateModel;
    private IFileModel fileModel;
    private MeEvaluateAdapter evaluateAdapter;
    public MeEvaluatePresenter(){
        evaluateModel = new EvaluateModel();
        fileModel = new FileModel();
    }
//    public void onCheckedChanged(Context context, int type) {
//        onCheckedChanged(context,type,true);
//
//    }

    public void onCheckedChanged(Context context, boolean showProgress) {
        if(showProgress){
            mView.showProgress();
        }

        Map<String,Object> param = new HashMap<>();
        param.put(KEY_TOKEN, ConfigValue.DATA_KEY);
        param.put(KEY_PAGE,0);
        param.put(KEY_PAGE_SIZE,DEFAULT_PAGE_SIZE);
        Map<String,Object> map = new HashMap<>();
        map.put(KEY_PHONE, SPUtils.get(context, SPConfig.PHONE,"").toString());
        param.put(KEY_PARAM,map);
            evaluateModel.loadGoodsEvaData(context,param,new GoodsEvaCallBack());
    }


    public void deleteFile(Context context, final int id, final int position, final BaseView.OnLoadListener listener) {
        Map<String,String> param = new HashMap<>();
        param.put(KEY_TOKEN,ConfigValue.DATA_KEY);
        param.put("fileId",String.valueOf(id));
        mView.showProgress();
        fileModel.deleteMallFile(context,param, new CallBack<BaseBean>() {
            @Override
            public void onSuccess(BaseBean response, Context context, Object tag) {
                hideProgress();
                if (response.getCode() == RESPONSE_CODE_SUC){
                    Object[] obj = new Object[3];
                    obj[0] = CALLBACK_TYPE_DELETE_PICTURE;
                    obj[1] = id;
                    obj[2] = position;
                    listener.onLoad(obj);
                }
            }

            @Override
            public void onFailure(String message) {
                hideProgress();
            }
        });
    }

    private void hideProgress() {
        if (mView != null){
            mView.hideProgress();
        }
    }

    private class GoodsEvaCallBack implements CallBack<EvaGoodsBean> {

        @Override
        public void onSuccess(EvaGoodsBean response, Context context, Object tag) {
            hideProgress();
            if (response.getCode() == RESPONSE_CODE_SUC){
                evaluateAdapter = new MeEvaluateAdapter(context,MeEvaluateAdapter.TYPE_GOODS,response);
                mView.loadData(evaluateAdapter);
            }

        }

        @Override
        public void onFailure(String message) {
            hideProgress();
        }
    }

    public void findGoodsAppraiseForCustomer(final Context context, String orderCode, final BaseView.OnLoadListener onLoadListener) {
        final Map<String,String> param = new HashMap<>();
        param.put(KEY_TOKEN,ConfigValue.DATA_KEY);
        param.put("orderCode",orderCode);
        mView.showProgress();
        evaluateModel.findGoodsAppraiseForCustomer(context,param, new CallBack<GoodsAppraiseInitResponse>() {
            @Override
            public void onSuccess(GoodsAppraiseInitResponse response, Context context, Object tag) {
                if (mView == null) return;
                hideProgress();
                if (response != null && response.getCode() == RESPONSE_CODE_SUC){
                    Object[] param = new Object[2];
                    param[0] = CALLBACK_TYPE_GOODS_DETAIL;
                    param[1] = response.getData();
                    onLoadListener.onLoad(param);
                }else{
                    Utils.showToast(context,"获取评论信息失败");
                    ((Activity)context).finish();
                }

            }

            @Override
            public void onFailure(String message) {
                if (mView == null) return;
                hideProgress();
                Utils.showToast(context,"获取评论信息失败");
                ((Activity)context).finish();
            }
        });
    }

    public void saveAppraiseForCustomer(final Context context, final List<GoodsAppraiseInitResponse.Data> listData, final BaseView.OnLoadListener listener) {
        Map<String,Object> param = new HashMap<>();
        param.put(KEY_TOKEN,ConfigValue.DATA_KEY);
        param.put("mallGoodsAppraiseList",listData);
        mView.showProgress();
        evaluateModel.saveAppraiseForCustomer(context,param, new CallBack<BaseBean>() {
            @Override
            public void onSuccess(BaseBean response, Context context, Object tag) {
                hideProgress();
                if (response != null && response.getCode() == RESPONSE_CODE_SUC){
                    listener.onLoad(response.getMessage());
                    Utils.showToast(context,"保存成功");
                }else if (response != null && !TextUtils.isEmpty(response.getMessage())){
                    Utils.showToast(context,response.getMessage());
                }else{
                    Utils.showToast(context,"保存失败");
                    ((Activity)context).finish();
                }
            }

            @Override
            public void onFailure(String message) {
                hideProgress();
                Utils.showToast(context,"保存失败");
                ((Activity)context).finish();
            }
        });
    }

    public void doUploadGoodsEvaluate(Context context, Uri uri, int busiId, final BaseView.OnLoadListener listener) {
        Bitmap bitmap = null;
        try {
            bitmap = Utils.getBitmapFormUri((Activity) context,uri);
            doUploadGoodsEvaluate(context,bitmap,busiId,listener);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public void doUploadGoodsEvaluate(Context context, Bitmap bitmap, int busiId, final BaseView.OnLoadListener listener) {
        Map<String,String> param = new HashMap<>();
        param.put(KEY_TOKEN,ConfigValue.DATA_KEY);
        param.put("fileId","0");
        param.put("busiId",String.valueOf(busiId));
        param.put("busiTypeId","goodsAppraise");
        param.put("fileName",System.currentTimeMillis() + ".jpg");
        param.put(KEY_FILE_BASE64_PIC, Utils.bitmapToBase64(bitmap));

        mView.showProgress();
        fileModel.uploadMallFile(context,param, new CallBack<FileBean>() {
            @Override
            public void onSuccess(FileBean response, Context context, Object tag) {
                hideProgress();
                if (response.getCode() == RESPONSE_CODE_SUC){
                    listener.onLoad(response.getData());
                }
            }

            @Override
            public void onFailure(String message) {
                hideProgress();
            }
        });
    }

    public void cardSelect(Activity activity) {
        Intent it = new Intent(Intent.ACTION_PICK);
        it.setType("image/*");
        activity.startActivityForResult(it, INTENT_SELECT_PIC);
    }
}
